package com.hqu.environment.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.hqu.environment.annotation.LoginUser;
import com.hqu.environment.dao.FarmCategoryDao;
import com.hqu.environment.dto.FarmCategoryDTO;
import com.hqu.environment.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @Description: 农场Controller
 * @Date: 2019/04/25 22:15
 * @Author: 邓勇嘉
 **/
@Controller
@RequestMapping("admin/farmCategory")
public class AdminFarmCategoryController {
    @Autowired
    FarmCategoryDao farmCategoryDao;


    @RequestMapping("/getList")
    @ResponseBody
    public Result getFarmCategoryList(FarmCategoryDTO farmCategoryDTO) {
        List<FarmCategoryDTO> farmCategoryList = farmCategoryDao.selectList(farmCategoryDTO);
        return Result.success().add("farmCategoryList",farmCategoryList);
    }

    @RequestMapping("/getById")
    @ResponseBody
    public Result getFarmCategoryById(Integer id) {
        FarmCategoryDTO farmCategoryDTO = farmCategoryDao.selectByPrimaryKey(id);
        return Result.success().add("farmCategory",farmCategoryDTO);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        farmCategoryDao.deleteByPrimaryKey(id);
        return Result.success();
    }


    @RequestMapping("/add")
    @ResponseBody
    public Result addFarmCategory(@LoginUser Integer userId, FarmCategoryDTO farmCategoryDTO) {
        farmCategoryDTO.setCreator(userId);
        farmCategoryDTO.setCreateTime(new Date());
        farmCategoryDao.insertSelective(farmCategoryDTO);
        return Result.success();
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result updateFarmCategory(@LoginUser Integer userId, FarmCategoryDTO farmCategoryDTO) {
        farmCategoryDTO.setCreator(userId);
        farmCategoryDTO.setCreateTime(new Date());
        farmCategoryDao.updateByPrimaryKeySelective(farmCategoryDTO);
        return Result.success();
    }

    @RequestMapping("/deleteList")
    @ResponseBody
    public Result deleteFarmCategoryList(String ids) {
        List <Integer> list = JSONObject.parseArray(ids,Integer.class);
        farmCategoryDao.delete(list);
        return Result.success();
    }
}
